/*
 * Created on 2005-8-24
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.store;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jedi.BaseObj;
import com.tss.util.DbConn;
import com.tss.util.DbRs;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ArticleMgr extends BaseObj {
	public ArticleMgr() {}
	public Article get(String id) {
		clearErr();
		Article article = new Article(id);
		DbConn conn = new DbConn();
		try {
			String sql = "";
			sql = "select a.*," +
				" b.category_name,b.parent_id,b.order_id,b.is_leaf," +
				" b.category_desc" +
				" from t_article_info a inner join t_article_category b " +
				" on a.category_id = b.category_id where a.article_id = ?";
			conn.prepare(sql);
			conn.setString(1,id);
			DbRs rs = conn.executeQuery();
			if (rs == null || rs.size() ==0) return null;
			ArticleCategory cate = new ArticleCategory(get(rs,0,"category_id"));
			cate.setCategoryName(get(rs,0,"category_name"));
			cate.setCategoryDesc(get(rs,0,"category_desc"));
			cate.setParentId(get(rs,0,"parent_id"));
			if (getInt(rs,0,"is_leaf") == 1)
				cate.setLeaf();
			cate.setOrderId(getInt(rs,0,"order_id"));

			article.setArticleTitle(get(rs,0,"article_title"));
			article.setAuthorId(get(rs,0,"author_id"));
			article.setAuthorName(get(rs,0,"author_name"));
			article.setArticleContent(get(rs,0,"article_content"));
			article.setCategoryId(get(rs,0,"category_id"));
			article.setIssueTime(get(rs,0,"issue_time"));
			article.setHit(getInt(rs,0,"hit"));
			article.setDocInfo(get(rs,0,"doc_info"));
			article.setCategory(cate);
			return article;
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
	}
	
	public List get(String otherSQL,String orderField) {
		clearErr();
		DbConn conn = new DbConn();
		DbRs rs = null;
		try {
			String sql = "";
			sql = "select a.*," +
				" b.category_name,b.parent_id,b.order_id,b.is_leaf," +
				" b.category_desc" +
				" from t_article_info a inner join t_article_category b " +
				" on a.category_id = b.category_id where 1=1";
			if (otherSQL != null && !otherSQL.trim().equals("")) 
				sql += otherSQL;
			if (orderField != null && !orderField.trim().equals(""))
				sql += " order by " + orderField;
			if (getTopNum() > 0)
				sql += " limit " + getTopNum();
			conn.prepare(sql);
			rs = conn.executeQuery();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
		if (rs != null && rs.size() >0) {
			List r = new ArrayList();
			for(int i=0;i<rs.size();i++) {
				ArticleCategory cate = new ArticleCategory(get(rs,i,"category_id"));
				cate.setCategoryName(get(rs,i,"category_name"));
				cate.setParentId(get(rs,i,"parent_id"));
				cate.setOrderId(getInt(rs,i,"order_id"));
				if (getInt(rs,i,"is_leaf") == 1)
					cate.setLeaf();
				cate.setCategoryDesc(get(rs,i,"category_desc"));
				Article article = new Article(get(rs,i,"article_id"));
				article.setArticleTitle(get(rs,i,"article_title"));
				article.setAuthorId(get(rs,i,"author_id"));
				article.setAuthorName(get(rs,i,"author_name"));
				article.setArticleContent(get(rs,i,"article_content"));
				article.setCategoryId(get(rs,i,"category_id"));
				article.setIssueTime(get(rs,i,"issue_time"));
				article.setHit(getInt(rs,i,"hit"));
				article.setDocInfo(get(rs,i,"doc_info"));
				article.setCategory(cate);
				r.add(article);
			}
			return r;
		}
		return null;
	}
	
	public List get(String otherSQL,String orderField,int pageNo, int pageSize) {
		clearErr();
		DbRs rs = null;
		DbConn conn = null;
		try {
			conn = new DbConn();
			String sqlCount = "select count(a.*) as total" +
				" from t_article_info a inner join t_article_category b " +
				" on a.category_id = b.category_id where 1=1";
			if (otherSQL != null && !otherSQL.trim().equals(""))
				sqlCount += otherSQL;
			conn.prepare(sqlCount);
			DbRs rsCount = conn.executeQuery();
			int total = getInt(rsCount,0,"total");
			setTotal(total);
			boolean b = total % pageSize == 0;
			int pageCount = b ? (total / pageSize) : (total / pageSize + 1);
			if (pageNo > pageCount) pageNo = pageCount;
			String sql = "select a.*," +
				" b.category_name,b.parent_id,b.order_id,b.is_leaf," +
				" b.category_desc" +
				" from t_article_info a inner join t_article_category b " +
				" on a.category_id = b.category_id where 1=1";
			if (otherSQL != null && !otherSQL.trim().equals(""))
				sql += otherSQL;
			if (orderField != null && !orderField.trim().equals(""))
				sql += " order by " + orderField;
			conn.prepare(sql);
			rs = conn.executeQuery(pageNo, pageSize);
		} catch (SQLException ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return new ArrayList();
		} catch (Exception e) {
			e.printStackTrace();
			setErr(e.getMessage());
			return new ArrayList();
		} finally {
			conn.close();
		}
		List r = new ArrayList();
		if (rs == null || rs.size() <= 0)
			return r;
		for (int i = 0; i < rs.size(); i++) {
			ArticleCategory cate = new ArticleCategory(get(rs,i,"category_id"));
			cate.setCategoryName(get(rs,i,"category_name"));
			cate.setParentId(get(rs,i,"parent_id"));
			cate.setOrderId(getInt(rs,i,"order_id"));
			if (getInt(rs,i,"is_leaf") == 1)
				cate.setLeaf();
			cate.setCategoryDesc(get(rs,i,"category_desc"));
			Article article = new Article(get(rs,i,"article_id"));
			article.setArticleTitle(get(rs,i,"article_title"));
			article.setAuthorId(get(rs,i,"author_id"));
			article.setAuthorName(get(rs,i,"author_name"));
			article.setArticleContent(get(rs,i,"article_content"));
			article.setCategoryId(get(rs,i,"category_id"));
			article.setIssueTime(get(rs,i,"issue_time"));
			article.setHit(getInt(rs,i,"hit"));
			article.setDocInfo(get(rs,i,"doc_info"));
			article.setCategory(cate);
			r.add(article);
		}
		return r;
	}
}
